using System;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using Academia.BI;
using Page=System.Web.UI.Page;

public partial class FotoEntry : Page
{
    private const string PHOTODIR = @"C:\Academia\Foto\";

    #region Load

    protected void Page_Load(object sender, EventArgs e)
    {
        GetFoto();
    }

    #endregion

    private void GetFoto()
    {
        try
        {
            SqlDataReader dr =
                SqlDataManager.GetReader("SELECT ISNULL(FotoPath, 'null.png') FROM VPessoa WHERE PessoaID = " +
                                         Request["key1"]);
            if (dr.Read())
                imgPessoa.ImageUrl = "../Foto/" + dr[0];
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void lnkUpload_Click(object sender, EventArgs e)
    {
        try
        {
            lblError.Text = String.Empty;
            if (fupMain.PostedFile != null)
            {
                if ((fupMain.PostedFile.ContentLength/1024) < 5120)
                {
                    if (
                        fupMain.PostedFile.ContentType == "image/gif" ||
                        fupMain.PostedFile.ContentType == "image/jpg" ||
                        fupMain.PostedFile.ContentType == "image/jpeg" ||
                        fupMain.PostedFile.ContentType == "image/pjpeg" ||
                        fupMain.PostedFile.ContentType == "image/png"
                        )
                    {
                        Upload();

                        lblError.Text = "Foto alterada com sucesso";
                        GetFoto();
                    }
                    else
                        lblError.Text = "Arquivo diferente dos formatos JPG, GIF ou PNG";
                }
                else
                    lblError.Text = "Arquivo maior que 5MB";
            }
            else
                lblError.Text = "Nenhum arquivo selecionado";
        }
        catch (Exception ex)
        {
            lblError.Text = "Erro ao fazer o upload: " + ex.Message;
        }
    }

    private void Upload()
    {
        lblError.Text = String.Empty;

        string tempPath = Server.MapPath("../FotoTemp/" + fupMain.FileName);
        fupMain.PostedFile.SaveAs(tempPath);

        var file = new FileInfo(tempPath);

        try
        {
            Image img = Image.FromFile(tempPath);

            string path = tempPath.Replace("Temp", String.Empty).Replace(fupMain.FileName,
                                                                         Request["key1"] + file.Extension);

            img.Save(path);

            string sqlCommand = String.Format("Update Pessoa SET FotoPath = '{0}' WHERE PessoaID = {1}",
                                              Request["key1"] + file.Extension, Request["key1"]);
            SqlDataManager.ExecuteNonQuery(sqlCommand);
        }
        finally
        {
            //file.Delete();
        }
    }
}